import 'package:boss/models/commodity.dart';
import 'package:boss/models/offers.dart';
import 'package:boss/ui/widget/form/select_form_field.dart';
import 'package:boss/util/time_util.dart';
import 'package:flustars/flustars.dart' as flustars;
import 'package:flutter/material.dart';

class BaseProvider extends ChangeNotifier {
  initData({Offers data}) {
    if (data != null) {
      offerName = data.activityName;
      activityType = data.activityType;
      if (activityType == 1) {
        if (data.weekTime != '') {
          weekdayRange = data.weekTime.split('|').map((item) => int.parse(item)).toList();
        }
        if (data.startDate != '') {
          var startDate = flustars.DateUtil.getDateTime(data.startDate);
          var endDate = flustars.DateUtil.getDateTime(data.endDate);

          dateRange = [startDate, endDate];
        }
        if (data.startTime != '') {
          hourRange = [TimeUtil.string2Int(data.startTime), TimeUtil.string2Int(data.endTime)];
        }
      }
    }
  }

  String offerName = '';
  List<SelectItem<int>> activityTypes = [
    SelectItem<int>(title: '永久有效', value: 0),
    SelectItem<int>(title: '自定义', value: 1),
  ];
  List<Commodity> commoditys = [];
  int activityType = 0;
  List<DateTime> dateRange;

  String get dateRangeText {
    return dateRange == null ? null : '${flustars.DateUtil.formatDate(dateRange[0], format: 'yyyy/MM/dd')}' + '-' + '${flustars.DateUtil.formatDate(dateRange[1], format: 'yyyy/MM/dd')}';
  }

  List<int> hourRange;

  String get hourRangeText {
    return hourRange == null ? null : TimeUtil.int2TimeString(hourRange[0]) + '-' + TimeUtil.int2TimeString(hourRange[1]);
  }

  List<int> weekdayRange;

  String get weekdayRangeText {
    if (weekdayRange == null) return null;

    var tempStr = '星期';
    List<String> weekdays = ['一', '二', '三', '四', '五', '六', '日'];
    weekdayRange.forEach((weekday) {
      tempStr += weekdays[weekday];
      if (weekdayRange.last != weekday) {
        tempStr += '、';
      }
    });

    return tempStr;
  }

  setActivityType(int value) {
    activityType = value;
    if (value == 1 && dateRange == null && hourRange == null && weekdayRange == null) {
      var now = DateTime.now();

      var defaultStartDateTime = DateTime(now.year, now.month, now.day);
      var defaultEndDateTime = defaultStartDateTime.add(Duration(days: 7));
      dateRange = [defaultStartDateTime, defaultEndDateTime];

      hourRange = [0, 24];

      weekdayRange = [0, 1, 2, 3, 4, 5, 6];
    }
    notifyListeners();
  }

  setOfferName(String value) {
    offerName = value;
    notifyListeners();
  }

  setCommoditys(List<Commodity> value) {
    commoditys = value;
    notifyListeners();
  }

  removeCommodity(Commodity value) {
    commoditys.remove(value);
    notifyListeners();
  }

  setDateRange(List<DateTime> value) {
    dateRange = value;
    notifyListeners();
  }

  setHourRange(List<int> value) {
    hourRange = value;
    notifyListeners();
  }

  setWeekRange(List<int> value) {
    weekdayRange = value;
    notifyListeners();
  }

  setDateTimeForHttpData(Map<String, dynamic> data) {
    if (activityType == 1) {
      if (dateRange != null) {
        data['storeDiscountStandard']['startDate'] = '${flustars.DateUtil.formatDate(dateRange[0], format: 'yyyy-MM-dd')}';
        data['storeDiscountStandard']['endDate'] = '${flustars.DateUtil.formatDate(dateRange[1], format: 'yyyy-MM-dd')}';
      }

      if (hourRange != null) {
        data['storeDiscountStandard']['startTime'] = '${TimeUtil.int2TimeString(hourRange[0])}:00';
        data['storeDiscountStandard']['endTime'] = '${TimeUtil.int2TimeString(hourRange[1])}:00';
      }

      if (weekdayRange != null) {
        data['storeDiscountStandard']['weekTime'] = weekdayRange.join('|');
      }
    }
  }
}
